package middlewares

import (
	"IMChat/utils"
	"github.com/gin-gonic/gin"
)

func AuthCheck() gin.HandlerFunc {
	return func(context *gin.Context) {
		token := context.GetHeader("token")
		userClaims, err := utils.ParseToken(token)
		if err != nil {
			context.Abort()
			res := new(utils.Response)
			res.Code = -1
			utils.FailWithMsg(context, res.Code, "User Auth Fail !")
			return
		}
		context.Set("user_claims", userClaims)
		context.Next()
	}
}
